package com.yoyo.yyadsdk.util;

import android.content.Context;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yoyo.yyadsdk.bean.dao.LockScreenDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnalysisUtils {

    //开始加载
    public static final int TYPE_LOAD = 1;
    //Click
    public static final int TYPE_CLICK = 2;
    //Start
    public static final int TYPE_READY = 3;
    //Success
    public static final int TYPE_START = 4;
    //Timeout
    public static final int TYPE_SUCCESS = 5;
    //Success
    public static final int TYPE_FINISH = 6;
    //Timeout
    public static final int TYPE_TIME_OUT = 7;
    //Lock
    public static final int POSITION_LOCK = 1;
    //Banner
    public static final int POSITION_BANNER = 2;

    /**
     * @param lockScreenId 内容类型
     * @param positionId   位置id
     * @param type         事件类型
     * @param contentId    内容id
     */
    public static void addStatistics(Context context, int lockScreenId, int positionId, int type, String contentId) {
        List<LockScreenDetail> list = new ArrayList<>();
        list.add(new LockScreenDetail(lockScreenId, type, positionId, contentId));
        Gson gson = new Gson();
        String s = gson.toJson(list);
        Map<String, String> map = new HashMap<>();
        String content = "timestamp=" + System.currentTimeMillis() + "&imei=" + SystemUtil.getIMEI(context) + "&lockscreenDetail=" + s;
        map.put("sign", Aes.encode(content, Aes.keyBytes));
        HttpUtils.doHttpRequest(ApiConstants.POST, ApiConstants.addLockScreenDetail, map, new HttpUtils.StringCallback() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d("数据上报成功:" + result);
            }

            @Override
            public void onFailure(int code, Exception e) {
                LogUtil.e("数据上报失败:" + code + e.getMessage());
            }
        });
    }

    public static void addTimeStatistics(int position, long ready, long start, long success, long finish) {
        if (ready < 0) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        String content = "timestamp=" + System.currentTimeMillis() + "&positionId=" + position + "&openTime=" + ready +
                "&loadTime=" + start + "&readTime=" + success + "&closeTime=" + finish;
        map.put("sign", Aes.encode(content, Aes.keyBytes));
        HttpUtils.doHttpRequest(ApiConstants.POST, ApiConstants.addContentTime, map, new HttpUtils.StringCallback() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d("数据上报成功:" + result);
            }

            @Override
            public void onFailure(int code, Exception e) {
                LogUtil.e("数据上报失败:" + code + e.getMessage());
            }
        });
    }

}
